Auto
Execute Mesh Example
Description
This
tool demonstrates how custom tools can implement Apex Automatic Execution
(Lightning bolt) behavior. The example extends the “Mesh selected bodies”
example to include automatic execution capabilities using the standard
lightning bolt that is present in many Apex tools.
·
When enabled (default), the tool will mesh each body as soon as
it is selected.
·
When disabled (by clicking on lightning bolt) the tool will mesh
the selected bodies only when the Apply button, or Middle Mouse Button (MMB) is
pressed
·
Modified picking using the Ctrl or Shift keys is also supported
Python Script Files
-
mesh_selected_bodies_auto_ui.py
-
mesh_selected_bodies.py
Significant Script
Features
Auto-execution behavior must be implemented in the UI script and
relies on new capabilities that expose selection “Events”. Event handling is a
software pattern that will cause the host application to call one or
more
registered
functions whenever a particular application Event, such a selection, keyboard
press, mouse movement occurs. The function can be registered to listen to some
subset of Events and typically must provide a defined number of input arguments
into which the host application will place Event data.
The
apex_sdk
module that supports the
UI portion of Apex custom tools has been extended to enable custom tools to
listen for and respond to Apex “Gesture” Events. Gesture Events include
selection,
mouse
and keyboard events. This capability
is accessed by defining a function within the UI script to respond to gesture
events and registering it with the Apex gesture even handler. The function name
is unimportant (you can call the function whatever you want) but the input
argument list is strictly defined and must be present. When a gesture Event is
issued by Apex, this function will be called
automatically
and the input arguments will contain the Event information which can then be
processed by your implementation of the function. All gesture events emitted by
Apex will cause this function to be called, each gesture identified by its name
making it quite easy to identify only those gesture events that you want to
process and ignore all others.
To support Apex automatic execution
behaviors
we are primarily interested in events that relate to changes in the number of
objects that are selected. In general, Apex will automatically execute a tool
method when some (tool specific) number of entities have been selected. In this
example, we will show how to automatically mesh the selected bodies as soon as
one (or more) bodies is selected.
mesh_selected_bodies_ui.py:
# MSC
Apex - User Defined Tools using Python Scripts
# Mesh
Selected Bodies UI Example
#
import
sys
import
os
import
apex_sdk
import
clr
# .NET
references
import
System
import
System.Windows.Controls as WPFControls
from
Microsoft.Win32 import OpenFileDialog
# Define
current file path of example
current_file_path
= os.path.dirname(os.path.realpath(__file__))
# Global variable to keep track of how
many objects are currently selected
global selected_entity_count
selected_entity_count = 0
# Global variable to keep track of pick
modifiers (Ctrl, Shift)
global selection_modifier_is_active
selection_modifier_is_active = False
# Set
pre-defined properties of ToolPropertyContainer
def
getUIContent():
# Make the tool property panel global
# We need to read the state of the Auto-execute button form other
functions
global my_toolProperty
my_toolProperty =
apex_sdk.ToolPropertyContainer()
# Provide an icon and a name for the tool
property panel
my_toolProperty.TitleImageUriString =
os.path.join(os.path.dirname(current_file_path), r"res\Robot_48.png")
my_toolProperty.TitleText = "Mesh
Selected Bodies"
# Enable auto execute (Lightning bolt) behavior
# This causes the lightning bolt button to be displayed at the top left
of the
# Tool property panel
my_toolProperty.IsAutoAppliedVisible = True
# Initialize the tool with Auto-execute active (default)
# Set this value to False if you want the tool to be displayed initially
in manual execution mode
my_toolProperty.IsAutoApplied = True
# Define UI
my_toolProperty.ToolPropertyContent =
getCustomToolPropertyContent()
# Handle apply button (green) click event
my_toolProperty.AppliedCommand =
apex_sdk.ActionCommand(System.Action(HandleApplyButton))
# Define PickFilterList
my_toolProperty.ShowPickChoice = True
my_toolProperty.PickFilterList =
setPickFilterList()
# Register a function to be called whenever “gesture” events occur
# Gesture events include mostly mouse, keyboard and selection activities
# HandleGesture is implemented below
apex_sdk.registerGestureHandler( HandleGesture )
return my_toolProperty
# Set PickFilters method
is unchanged and not shown here
# This method is called whenever a
gesture event occurs
# You can name this method whatever you
want and register it with the gesture
# handler above, but it MUST support
the two arguments shown below
@apex_sdk.errorhandler
def HandleGesture(
ToolGestureEventData, args ):
global selection_modifier_is_active
global selected_entity_count
# ignore the Mouse and Continue Gestures...
if "Mouse" in ToolGestureEventData.ges.gestureName:
return
if "Continue" in ToolGestureEventData.ges.gestureName:
return
# The KeyPress event is fired if any keyboard key is pressed
# Check if Ctrl or Shift keys are pressed and cache state in
# the global variable "selection_modifier_is_active"
# We'll use this state elsewhere to avoid automatically executing the
tool while these keys are pressed
if "KeyPress" in ToolGestureEventData.ges.gestureName:
if ToolGestureEventData.ges.modifiers in [2, 4]:
selection_modifier_is_active = True
return
# Check if any modifier keys are released
# Reset the cached value of "selection_modifier_is_active"
# Execute the ApplyButton if there are any selected entities AND
auto-execute is active
if "KeyRelease" in ToolGestureEventData.ges.gestureName:
if ToolGestureEventData.ges.modifiers in [0]:
selection_modifier_is_active =
False
if selected_entity_count > 0 and
my_toolProperty.IsAutoApplied == True :
HandleApplyButton()
return
# The SingleSelection event is fired when a single LMB pick happens
# Update the value of "selected_entity_count"
# If,
#
1 or more entities are
currently selected, AND
#
2) Auto-execute mode is
active, AND
#
3) No pick modifiers (Ctrl,
Shift) are currently pressed
# Then
#
execute the Apply Button
function
if "SingleSelection" in ToolGestureEventData.ges.gestureName:
selected_entity_count =
ToolGestureEventData.ges.currentSelectionList.count()
if selected_entity_count > 0 and my_toolProperty.IsAutoApplied ==
True and not selection_modifier_is_active:
HandleApplyButton()
return
# Apex fires the "RectangleSelect" event whenever a box pick
happens
# Update the value of "selected_entity_count"
# This event uses pretty much the same logic as the SingleSelection
event handler to determine
# whether the Apply function should be called
if "RectangleSelect" in ToolGestureEventData.ges.gestureName:
selected_entity_count = ToolGestureEventData.ges.currentSelectionList.count()
if selected_entity_count > 0 and my_toolProperty.IsAutoApplied ==
True and not selection_modifier_is_active:
HandleApplyButton()
return
# Apex fires the "SelectionExtended" event whenever the
current selection is extended
# Basically, if picking is hppening while the Ctrl or Shift keys are
pressed
# Updates the value of "selected_entity_count"
# This event uses pretty much the same logic as the SingleSelection event
handler to determine
# whether the Apply function should be called
if "SelectionExtended" in
ToolGestureEventData.ges.gestureName:
selected_entity_count =
ToolGestureEventData.ges.currentSelectionList.count()
if selected_entity_count > 0 and my_toolProperty.IsAutoApplied ==
True and not selection_modifier_is_active:
HandleApplyButton()
return
# Apex fires the "SelectionEmpty" or
"SelectionCleared" events whenever the current selection is cleared
# Update the value of "selected_entity_count to 0
if ToolGestureEventData.ges.gestureName in ["SelectionEmpty",
"SelectionCleared"]:
selected_entity_count = 0
return
getCustomToolPropertyContent(): method is unchanged and not
shown here
HandleApplyButton():method is unchanged and not shown here
mesh_selected_bodies.py:
No changes are required to
the script that executes the tool function to enable auto-execution behavior
- Log in to post comments